package com.ebelter.btcomlib.models.bluetooth.products.bpm;


import android.os.Message;
import android.text.TextUtils;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.bean.BpmMesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.bpm.interfaces.BPMMeasureCallback;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Arrays;

import static com.ebelter.btcomlib.models.bluetooth.products.bpm.BPMConstant.BPM_NAME;

public class BPMBytesAnalysis extends BaseHandle {

    private static final String TAG = "BPMBytesAnalysis";


    private static final int WHAT_DynamicSystolicPressure = 1;
    private static final int WHAT_BpmMesureResult = 2;
    private static final int WHAT_Error = 3;

    //  private byte[] lastReceiveMesureBytes0;//接收到的最后最后字节数组
    private byte[] lastReceiveMesureBytes;//接收到的最后最后字节数组
    private BPMMeasureCallback bpmMeasureCallback;

    void setBpmMeasureCallback(BPMMeasureCallback bpmMeasureCallback) {
        this.bpmMeasureCallback = bpmMeasureCallback;
    }

    @Override
    public void handleMsg(Message msg) {
        if (msg.what == WHAT_DynamicSystolicPressure) {
            LogUtils.i(TAG, "解析到动态收缩压的值 =" + msg.arg1);
            if (bpmMeasureCallback != null) bpmMeasureCallback.dynamicSystolicPressure(msg.arg1);
        } else if (msg.what == WHAT_Error) {
            LogUtils.i(TAG, "解析到错误码 =" + msg.arg1);
            if (bpmMeasureCallback != null) bpmMeasureCallback.measureError(connectName, msg.arg1);
        } else if (msg.what == WHAT_BpmMesureResult) {
            BpmMesureResult b = (BpmMesureResult) msg.obj;
            LogUtils.i(TAG, "解析到测量结果 =" + b);
            if (bpmMeasureCallback != null) bpmMeasureCallback.measureResult(b);
        }
    }


    //分析结果
    void resultAnalysis(byte[] data) {
        LogUtils.i(TAG, "connectName = " + connectName + "---BPMBytesAnalysis接收到信息：" + BytesUtils.bytes2HexStr(data));
        if (TextUtils.equals(BPMConstant.BPM_NAME, connectName)) {
            jiexiBPM_NAME1(data);
        } else if (TextUtils.equals(BPMConstant.BPM_NAME2, connectName)) {
            jiexiBPM_NAME2(data);
        }
    }

    private void jiexiBPM_NAME2(byte[] data) {
        if (data == null || data.length == 0) return;

        if (data.length == 7 && data[0] == 0x02 && data[2] == (byte) 0xd0) {//动态值
            sendMessageValue(WHAT_DynamicSystolicPressure, BytesUtils.getIntFrom2Byte((byte) 0, data[5]));//AC
            lastReceiveMesureBytes = null;
            return;
        }


        if (data.length == 9 && data[0] == 0x02 && data[2] == (byte) 0xd3) {//测脂出错

            if (Arrays.equals(lastReceiveMesureBytes, data)) {
                LogUtils.i(TAG, "BPMBytesAnalysis接收到重复的数据被摒弃掉");
            } else {
                lastReceiveMesureBytes = data;
                LogUtils.i(TAG, "BPMBytesAnalysis--接收测量--有异常：" + data[7]);
                sendMessageValue(WHAT_Error, BytesUtils.getIntFrom2Byte((byte) 0, data[7]));
            }

        }
        if (data.length == 17 && data[0] == 0x02 && data[2] == (byte) 0xd1) {//测量完整


            if (Arrays.equals(lastReceiveMesureBytes, data)) {
                LogUtils.i(TAG, "BPMBytesAnalysis接收到重复的数据被摒弃掉");
            } else {
                lastReceiveMesureBytes = data;
                BpmMesureResult bpmMesureResult = new BpmMesureResult();

                // UserSpUtil.writeLong(Constants.IUser.USER_ID,useridlong);

                bpmMesureResult.mesureUnit = BytesUtils.getByteHLByIndex(data[4], 0);
                bpmMesureResult.mesureStation = BytesUtils.getByteHLByIndex(data[4], 5);

                bpmMesureResult.systolicPressure = BytesUtils.getIntFrom2Byte(data[5], data[6]);
                bpmMesureResult.diastolicPressure = BytesUtils.getIntFrom2Byte(data[7], data[8]);
                bpmMesureResult.pulse = BytesUtils.getIntFrom2Byte(data[11], data[12]);
                bpmMesureResult.testTime = TimeUtils.getCurrentTime1();

                removeMessages(WHAT_BpmMesureResult);
                sendMessageObj(WHAT_BpmMesureResult, bpmMesureResult);
            }


        }


    }

    private void jiexiBPM_NAME1(byte[] data) {
        if (data == null || data.length == 0) {
            return;
        }

        if (data.length == 2 && data[0] == 0x20) {//说明接收到动态收缩压的值
            lastReceiveMesureBytes = null;
            sendMessageValue(WHAT_DynamicSystolicPressure, BytesUtils.getIntFrom2Byte((byte) 0, data[1]));
            return;

        }

//[-1, -2, -1, 5]
//BpmManager--IDataReadWriteCallback-onCharacteristicChanged--读到-[FF FE FF 05]
        if (data.length == 4 && (data[0] == (byte) 0xff) && (data[1] == (byte) 0xfe) && data[2] == (byte) 0xff) {
            //[FF FE FF 05]
            LogUtils.i(TAG, "BPMBytesAnalysis--接收测量--有异常：" + data[3]);
            if (Arrays.equals(lastReceiveMesureBytes, data)) {
                LogUtils.i(TAG, "BPMBytesAnalysis接收到重复的数据被摒弃掉");
            } else {
                lastReceiveMesureBytes = data;
                sendMessageValue(WHAT_Error, BytesUtils.getIntFrom2Byte((byte) 0, data[3]));
            }

            return;

        }


        if (data.length == 12) {//测量结果


            if (Arrays.equals(lastReceiveMesureBytes, data)) {
                LogUtils.i(TAG, "BPMBytesAnalysis接收到重复的数据被摒弃掉");
            } else {
                lastReceiveMesureBytes = data;
                BpmMesureResult bpmMesureResult = new BpmMesureResult();

                // bpmMesureResult.userID= UserSpUtil.readLong(Constants.IUser.USER_ID);
                // UserSpUtil.writeLong(Constants.IUser.USER_ID,useridlong);

                bpmMesureResult.mesureUnit = BytesUtils.getByteHLByIndex(data[0], 0);
                bpmMesureResult.mesureStation = BytesUtils.getByteHLByIndex(data[0], 5);


                bpmMesureResult.systolicPressure = BytesUtils.getIntFrom2Byte(data[1], data[2]);
                bpmMesureResult.diastolicPressure = BytesUtils.getIntFrom2Byte(data[3], data[4]);
                bpmMesureResult.pulse = BytesUtils.getIntFrom2Byte(data[7], data[8]);
                bpmMesureResult.testTime = TimeUtils.getCurrentTime1();

                removeMessages(WHAT_BpmMesureResult);

                sendMessageObj(WHAT_BpmMesureResult, bpmMesureResult);

            }


        }
    }

    private String connectName;//蓝牙连接的名字

    public void setConnectName(String name) {
        connectName = name;
    }
}
